using Abp.Authorization.Users;
using Abp.Domain.Entities.Auditing;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using Yozeev.BusinessLogic;
using Yozeev.EntityFrameworkCore;
using Yozeev.Help;
using Yozeev.SystemConfig;
using Yozeev.SystemConfig.BasicConfig;
using Yozeev.UserManagerment.Users;
using Microsoft.Extensions.Options;
using Yozeev.Authorization.Roles;

namespace Yozeev.Seed.Tenants
{
    public class YozeevCreator
    {
        private readonly YozeevDbContext _context;

        public YozeevCreator(YozeevDbContext context)
        {
            _context = context;
        }

        public void Create()
        {
            CreateSeed();
        }

        private void CreateSeed()
        {
            //创建微信用户
            var adminRole = _context.Roles.IgnoreQueryFilters()
                .FirstOrDefault(r => r.TenantId == 1 && r.Name == StaticRoleNames.Tenants.Admin);
            var userList = _context.Users.ToList();
            if (userList.Where(m=>m.UserName== StaticRoleNames.Host.Wechat).ToList().Count<=0)
            {
                var wechat = User.CreateTenantAdminUser(1, "ltm@ddxc.org", StaticRoleNames.Host.Wechat);
                wechat.Password =
                    new PasswordHasher<User>(new OptionsWrapper<PasswordHasherOptions>(new PasswordHasherOptions()))
                        .HashPassword(wechat, User.DefaultPassword);
                wechat.IsEmailConfirmed = true;
                wechat.IsActive = true;
                wechat.NeedToChangeThePassword = false;
                _context.Users.Add(wechat);
                _context.SaveChanges();
                _context.UserRoles.Add(new UserRole(1, wechat.Id, adminRole.Id));
                _context.SaveChanges();
            }

            //创建官网用户
            if (userList.Where(m => m.UserName == StaticRoleNames.Host.Web).ToList().Count <= 0)
            {
                var Web = User.CreateTenantAdminUser(1, "Web@ddxc.org", StaticRoleNames.Host.Web);
                Web.Password =
                    new PasswordHasher<User>(new OptionsWrapper<PasswordHasherOptions>(new PasswordHasherOptions()))
                        .HashPassword(Web, User.DefaultPassword);
                Web.IsEmailConfirmed = true;
                Web.IsActive = true;
                Web.NeedToChangeThePassword = false;
                _context.Users.Add(Web);
                _context.SaveChanges();
                _context.UserRoles.Add(new UserRole(1, Web.Id, adminRole.Id));
                _context.SaveChanges();
            }


            //菜单列表插入
            var menuList = _context.Menus.ToList();
            if (menuList.Count <= 0)
            {
                var menu = new Menu()
                {
                    MenuName = "工作台",
                    MenuCode = "Dashboard",
                    NavigateUrl = "/app/main/dashboard",
                    IocName = "anticon anticon-dashboard",
                    IocClassName = "anticon anticon-dashboard",
                    IsEnabled = true,
                    Sort = 1,
                    Level = 1
                };
                _context.Menus.Add(menu);
                _context.SaveChanges();
                menu = new Menu()
                {
                    MenuName = "管理",
                    MenuCode = "Administration",
                    NavigateUrl = "",
                    IocName = "anticon anticon-appstore",
                    IocClassName = "anticon anticon-appstore",
                    IsEnabled = true,
                    Sort = 4,
                    Level = 1
                };
                _context.Menus.Add(menu);
                _context.SaveChanges();
                var temp = _context.Menus.FirstOrDefault(m => m.MenuCode == "Administration");
                menu = new Menu()
                {
                    ParentId = temp.Id,
                    MenuName = "组织单元",
                    MenuCode = "OrganizationUnits",
                    NavigateUrl = "/app/admin/organization-units",
                    IocName = "anticon anticon-team",
                    IocClassName = "anticon anticon-team",
                    IsEnabled = true,
                    Sort = 1,
                    Level = 2
                };
                _context.Menus.Add(menu);
                _context.SaveChanges();
                menu = new Menu()
                {
                    ParentId = temp.Id,
                    MenuName = "角色",
                    MenuCode = "Roles",
                    NavigateUrl = "/app/admin/roles",
                    IocName = "anticon anticon-safety",
                    IocClassName = "anticon anticon-safety",
                    IsEnabled = true,
                    Sort = 2,
                    Level = 2
                };
                _context.Menus.Add(menu);
                _context.SaveChanges();
                menu = new Menu()
                {
                    ParentId = temp.Id,
                    MenuName = "用户",
                    MenuCode = "Users",
                    NavigateUrl = "/app/admin/users",
                    IocName = "anticon anticon-user",
                    IocClassName = "anticon anticon-user",
                    IsEnabled = true,
                    Sort = 3,
                    Level = 2
                };
                _context.Menus.Add(menu);
                _context.SaveChanges();
                menu = new Menu()
                {
                    ParentId = temp.Id,
                    MenuName = "语言列表",
                    MenuCode = "Languages",
                    NavigateUrl = "/app/admin/languages",
                    IocName = "anticon anticon-global",
                    IocClassName = "anticon anticon-global",
                    IsEnabled = true,
                    Sort = 4,
                    Level = 2
                };
                _context.Menus.Add(menu);
                _context.SaveChanges();
                menu = new Menu()
                {
                    ParentId = temp.Id,
                    MenuName = "审计日志",
                    MenuCode = "AuditLogs",
                    NavigateUrl = "/app/admin/auditLogs",
                    IocName = "anticon anticon-book",
                    IocClassName = "anticon anticon-book",
                    IsEnabled = true,
                    Sort = 5,
                    Level = 2
                };
                _context.Menus.Add(menu);
                _context.SaveChanges();
                menu = new Menu()
                {
                    ParentId = temp.Id,
                    MenuName = "维护",
                    MenuCode = "Maintenance",
                    NavigateUrl = "/app/admin/maintenance",
                    IocName = "anticon anticon-setting",
                    IocClassName = "anticon anticon-setting",
                    IsEnabled = true,
                    Sort = 6,
                    Level = 2
                };
                _context.Menus.Add(menu);
                _context.SaveChanges();
                menu = new Menu()
                {
                    ParentId = temp.Id,
                    MenuName = "设置",
                    MenuCode = "Settings",
                    NavigateUrl = "/app/admin/host-settings",
                    IocName = "anticon anticon-setting",
                    IocClassName = "anticon anticon-setting",
                    IsEnabled = true,
                    Sort = 7,
                    Level = 2
                };
                _context.Menus.Add(menu);
                _context.SaveChanges();
                menu = new Menu()
                {
                    ParentId = temp.Id,
                    MenuName = "调度管理",
                    MenuCode = "HangfireSchedule",
                    NavigateUrl = "/app/admin/hangfire",
                    IocName = "anticon anticon-setting",
                    IocClassName = "anticon anticon-setting",
                    IsEnabled = true,
                    Sort = 8,
                    Level = 2
                };
                _context.Menus.Add(menu);
                _context.SaveChanges();
                menu = new Menu()
                {
                    MenuName = "微信管理",
                    MenuCode = "WechatManagement",
                    NavigateUrl = "",
                    IocName = "anticon anticon-info-circle",
                    IocClassName = "anticon anticon-info-circle",
                    IsEnabled = true,
                    Sort = 5,
                    Level = 1
                };
                _context.Menus.Add(menu);
                _context.SaveChanges();
                temp = _context.Menus.FirstOrDefault(m => m.MenuCode == "WechatManagement");
                menu = new Menu()
                {
                    ParentId = temp.Id,
                    MenuName = "微信公众号",
                    MenuCode = "WechatAppConfig",
                    NavigateUrl = "/app/wechat/wechat-app-config",
                    IocName = "anticon anticon-dashboard",
                    IocClassName = "anticon anticon-dashboard",
                    IsEnabled = true,
                    Sort = 1,
                    Level = 2
                };
                _context.Menus.Add(menu);
                _context.SaveChanges();
                menu = new Menu()
                {
                    MenuName = "演示UI功能",
                    MenuCode = "DemoManagement",
                    NavigateUrl = "/app/demo/demoui",
                    IocName = "anticon anticon-info-circle",
                    IocClassName = "anticon anticon-info-circle",
                    IsEnabled = true,
                    Sort = 6,
                    Level = 1
                };
                _context.Menus.Add(menu);
                _context.SaveChanges();
                menu = new Menu()
                {
                    MenuName = "关于",
                    MenuCode = "About",
                    NavigateUrl = "/app/main/about",
                    IocName = "anticon anticon-info-circle",
                    IocClassName = "anticon anticon-info-circle",
                    IsEnabled = true,
                    Sort = 6,
                    Level = 1
                };
                _context.Menus.Add(menu);

                _context.SaveChanges();
            }
            //权限插入
            menuList = _context.Menus.ToList();
            var powerList = _context.Powers.ToList();
            if (powerList.Count <= 0)
            {
                var power = new Power()
                {
                    MenuId = _context.Menus.FirstOrDefault(m => m.MenuCode == "Roles").Id,
                    PowerName = "拥有菜单",
                    PowerCode = "ownmenu",
                    Sort = 1,
                    Category = PowerTypeEnum.Data,
                    IocClassName = "",
                    MenuPath = "",
                    GroupName = "",
                    Method = "",
                    Params = "",
                };
                _context.Powers.Add(power);
                _context.SaveChanges();
            }
            powerList = _context.Powers.ToList();
            var powerRoleList = _context.PowerRoles.ToList();
            if (powerRoleList.Count <= 0)
            {
                var powerRole = new PowerRole()
                {
                    PowerId = powerList.FirstOrDefault().Id,
                    RoleId = _context.Roles.LastOrDefault().Id
                };
                _context.PowerRoles.Add(powerRole);
                _context.SaveChanges();
            }
            //支付方式插入
            var payMethodList = _context.PayMethods.ToList();
            if (payMethodList.Count <= 0)
            {
                var PayMethod = new PayMethod()
                {
                    PayName = "现金",
                    PayCode = "Cash",
                    Sort = 1,
                    IsEnabled = true
                };
                _context.PayMethods.Add(PayMethod);
                _context.SaveChanges();
            }
            if(payMethodList.Count==1)
            {
                var PayMethod = new PayMethod()
                {
                    PayName = "微信",
                    PayCode = "Wechat",
                    Sort = 2,
                    IsEnabled = true
                };
                _context.PayMethods.Add(PayMethod);
                _context.SaveChanges();
                PayMethod = new PayMethod()
                {
                    PayName = "支付宝",
                    PayCode = "Alipay",
                    Sort = 3,
                    IsEnabled = true
                };
                _context.PayMethods.Add(PayMethod);
                _context.SaveChanges();
            }


            //机构插入
            var branchList = _context.Branchs.ToList();
            if (branchList.Count <= 0)
            {
                var branch = new Branch()
                {
                    BranchName = "海上观光游船系统",
                    BranchCode = "System",
                    Sort = 1,
                    Level = 1,
                    IsEnabled = true,

                };
                _context.Branchs.Add(branch);
                _context.SaveChanges();
                branchList = _context.Branchs.ToList();
                branch = new Branch()
                {
                    BranchName = "售票处",
                    BranchCode = "Sale",
                    Sort = 1,
                    Level = 2,
                    IsEnabled = true,
                    ParentId = branchList.FirstOrDefault().Id
                };
                _context.Branchs.Add(branch);
                branch = new Branch()
                {
                    BranchName = "财务处",
                    BranchCode = "Account",
                    Sort = 2,
                    Level = 2,
                    IsEnabled = true,
                    ParentId = branchList.FirstOrDefault().Id
                };
                _context.Branchs.Add(branch);
                _context.SaveChanges();
            }
            //机构用户插入
            var branchUserList = _context.BranchUsers.ToList();
            branchList = _context.Branchs.ToList();
            var user = _context.Users.Last();
            if (branchUserList.Count<=0)
            {
                var branchUser = new BranchUser()
                {
                    BranchId = branchList.FirstOrDefault().Id,
                    UserId = user.Id,
                };
                _context.BranchUsers.Add(branchUser);
                _context.SaveChanges();
            }
            //游客信息插入
            var customerList = _context.Customers.ToList();
            var usersList = _context.Users.ToList();
            branchList = _context.Branchs.ToList();
            if (customerList.Count <= 0)
            {
                var Customer = new Customer()
                {
                    CustomerName = "zhangsan",
                    Mobile = "13707330245",
                    LoginPwd = "123456",
                    Sex = SexEnum.Man,
                    Country = "中国",
                    Province = "湖南",
                    City = "长沙",
                    County = "",
                    CertificatesNum = "441223155512151855",
                    VerifiableType = VerifiableTypeEnum.ICCard,
                    Job = "白领",
                    OpenID = "123456",
                    Photo = "",
                    DateOfBirth = Convert.ToDateTime("1988-01-01"),
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id
                };
                _context.Customers.Add(Customer);
                Customer = new Customer()
                {
                    CustomerName = "李四",
                    Mobile = "137123454654",
                    LoginPwd = "123456",
                    Sex = SexEnum.Man,
                    Country = "中国",
                    CertificatesNum = "441256465461801",
                    VerifiableType = VerifiableTypeEnum.ICCard,
                    Photo = "",
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id
                };
                _context.Customers.Add(Customer);
                Customer = new Customer()
                {
                    CustomerName = "王五",
                    Mobile = "188123454654",
                    LoginPwd = "123456",
                    Sex = SexEnum.Woman,
                    Country = "中国",
                    CertificatesNum = "441256465461802",
                    VerifiableType = VerifiableTypeEnum.ICCard,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id
                };
                _context.Customers.Add(Customer);
                Customer = new Customer()
                {
                    CustomerName = "陆六",
                    Mobile = "188123454654",
                    LoginPwd = "123456",
                    Sex = SexEnum.Woman,
                    Country = "中国",
                    CertificatesNum = "441256465461803",
                    VerifiableType = VerifiableTypeEnum.ICCard,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id
                };
                _context.Customers.Add(Customer);
                _context.SaveChanges();

            }

            //旅行社信息插入
            var travelAgencyList = _context.TravelAgencys.ToList();
            usersList = _context.Users.ToList();
            if (travelAgencyList.Count <= 0)
            {
                var travelAgency = new TravelAgency()
                {
                    TravelName = "ABP旅行社",
                    Mobile = "13707330245",
                    Contacts = "zhangsan",
                    IsEnabled = true,
                    Discount = Convert.ToDecimal(0.8),
                    PayMethod = PayMethodEnum.PaySuccess,
                    TotalSum = 15,
                    PrepaidAmount = 10,
                    AlarmAmount = 10,
                    UpperLimit = 120,
                    Lowerlimit = 100,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id
                };
                _context.TravelAgencys.Add(travelAgency);
                _context.SaveChanges();

            }


            //充值记录插入
            travelAgencyList = _context.TravelAgencys.ToList();
            payMethodList = _context.PayMethods.ToList();
            var rechargeRecordList = _context.RechargeRecords.ToList();
            if (rechargeRecordList.Count <= 0)
            {
                var rechargeRecord = new RechargeRecord()
                {
                    TravelAgencyId = travelAgencyList.FirstOrDefault().Id,
                    Amount = 300,
                    RechargeStatus = RechargeStatusEnum.Cancel,
                    PayMethodId = payMethodList.FirstOrDefault().Id,
                };
                _context.RechargeRecords.Add(rechargeRecord);
                _context.SaveChanges();

            }

            //景区插入
            var scenicSpotList = _context.ScenicSpots.ToList();
            usersList = _context.Users.ToList();
            if (scenicSpotList.Count <= 0)
            {
                var scenicSpot = new ScenicSpot()
                {
                    ScenicSpotName = "海上观光游船",
                    ScenicSpotInfo = "海上观光游船",
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                    //ParentId = scenicSpotList.FirstOrDefault().Id,
                };
                _context.ScenicSpots.Add(scenicSpot);
                _context.SaveChanges();
                scenicSpotList = _context.ScenicSpots.ToList();
                scenicSpot = new ScenicSpot()
                {
                    ScenicSpotName = "海上观光游船",
                    ScenicSpotInfo = "海上观光游船",
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                    ParentId = scenicSpotList.FirstOrDefault().Id,
                };
                _context.ScenicSpots.Add(scenicSpot);
                _context.SaveChanges();
            }


            //销售源插入
            var sourceList = _context.Sources.ToList();
            usersList = _context.Users.ToList();
            if (sourceList.Count <= 0)
            {
                var source = new Source()
                {
                    SourceName = "前台",
                    SourceCode = "Reception",
                    Discount = 1,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.Sources.Add(source);
                source = new Source()
                {
                    SourceName = "微信",
                    SourceCode = "Wechat",
                    Discount = 1,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.Sources.Add(source);
                source = new Source()
                {
                    SourceName = "官网",
                    SourceCode = "Web",
                    Discount = 1,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.Sources.Add(source);
                source = new Source()
                {
                    SourceName = "售票机",
                    SourceCode = "TicketMachine",
                    Discount = 1,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.Sources.Add(source);
                source = new Source()
                {
                    SourceName = "自助机",
                    SourceCode = "SelfhelpMachine",
                    Discount = 1,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                source = new Source()
                {
                    SourceName = "闸机",
                    SourceCode = "GateMachine",
                    Discount = 1,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.Sources.Add(source);
                source = new Source()
                {
                    SourceName = "手持机",
                    SourceCode = "HandMachine",
                    Discount = 1,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.Sources.Add(source);
                _context.SaveChanges();

            }


            //航线插入
            var routeList = _context.Routes.ToList();
            usersList = _context.Users.ToList();
            if (routeList.Count <= 0)
            {
                var route = new Route()
                {
                    RouteName = "标准航线",
                    LineCode = 1,
                    LineInfo = "swei2020",
                    LineImage = "Image/a.jpg",
                    StartPosition = "起点",
                    EndPosition = "终点",
                    AuditStatus = AuditStatusEnum.RejecteAudit,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.Routes.Add(route);
                _context.SaveChanges();

            }


            //游船插入
            var boatList = _context.Boats.ToList();
            usersList = _context.Users.ToList();
            if (boatList.Count <= 0)
            {
                var boat = new Boat()
                {
                    BoatName = "海峡号",
                    BoatCode = "HX2020",
                    SeatNumber = 200,
                    RunStatus = RunStatusEnum.Maintenance,
                    Leader = "zhang",
                    LeaderMobile = "13707078212",
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.Boats.Add(boat);
                _context.SaveChanges();
            }


            //航班插入
            var scheduleList = _context.Schedules.ToList();
            boatList = _context.Boats.ToList();
            routeList = _context.Routes.ToList();
            usersList = _context.Users.ToList();
            if (scheduleList.Count <= 0)
            {
                var schedule = new Schedule()
                {
                    RouteId = routeList.FirstOrDefault().Id,
                    BoatId = boatList.FirstOrDefault().Id,
                    SaleDate = DateTime.Now,
                    StartTime = DateTime.Now,
                    EndTime = DateTime.Now.AddMonths(1),
                    SeatQuantity = 400,
                    SurplusQuantity = 100,
                    ScheduleStatus = ScheduleStatusEnum.WaitCheck,
                    AuditStatus = AuditStatusEnum.RejecteAudit,
                    CheckEndTime = DateTime.Now,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.Schedules.Add(schedule);
                _context.SaveChanges();
            }

            //设备插入
            var devicesList = _context.Devices.ToList();
            var ticketStationList = _context.TicketStations.ToList();
            if (devicesList.Count <= 0)
            {
                var device = new Device()
                {
                    TicketStationId = "",//ticketStationList.FirstOrDefault().StationName,
                    DeviceName = "yi",
                    DeviceCode = "",
                    DeviceStatus = DeviceStatusEnum.Closing,
                    IPAddress = "190.02.02.02",
                    Port = "1245",
                    DeviceType = DeviceTypeEnum.FaceMachine,
                    SecretKey = "123456",
                    IsEnabled = true,
                    CreationTime = DateTime.Now,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.Devices.Add(device);
                _context.SaveChanges();
            }


            //闸机记录插入
            var GateRecordList = _context.GateRecords.ToList();
            devicesList = _context.Devices.ToList();
            if (GateRecordList.Count <= 0)
            {
                var gateRecord = new GateRecord()
                {
                    DeviceId = devicesList.FirstOrDefault().Id,
                    VerifiableType = VerifiableTypeEnum.ICCard,
                    VerifiableValue = "a",
                    ReturnState = ReturnStateEnum.False,
                    ReturnInfo = "返回",
                    CreationTime = DateTime.Now,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.GateRecords.Add(gateRecord);
                _context.SaveChanges();
            }


            //可验证设置插入
            var verifiableSetList = _context.VerifiableSets.ToList();
            devicesList = _context.Devices.ToList();
            if (verifiableSetList.Count <= 0)
            {
                var verifiableSet = new VerifiableSet()
                {
                    DeviceId = devicesList.FirstOrDefault().Id,
                    VerifiableType = VerifiableTypeEnum.ICCard,
                    IsEnabled = true,
                    CreationTime = DateTime.Now,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.VerifiableSets.Add(verifiableSet);
                _context.SaveChanges();
            }

            //票种插入
            var ticketList = _context.Tickets.ToList();
            usersList = _context.Users.ToList();
            if (ticketList.Count <= 0)
            {
                var ticket = new Ticket()
                {
                    TicketName = "成人票",
                    TicketMode = TicketModeEnum.OnebyOne,
                    TicketType = TicketTypeEnum.Card,
                    CheckMethod = CheckMethodEnum.BalanceCheck,
                    TicketClassify = TicketClassifyEnum.Adult,
                    CheckNum = 400,
                    EffectiveLong = 5,
                    UnitPrice = 100,
                    TicketLength = 18,
                    UpperLimit = 120,
                    LowerLimit = 80,
                    AudioName = "sw",
                    AuditStatus = AuditStatusEnum.RejecteAudit,
                    IsEnabled = true,
                    Sort = 1,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.Tickets.Add(ticket);
                _context.SaveChanges();
            }

            //票价插入
            scheduleList = _context.Schedules.ToList();
            ticketList = _context.Tickets.ToList();
            var ticketPriceList = _context.TicketPrice.ToList();
            usersList = _context.Users.ToList();
            if (ticketPriceList.Count <= 0)
            {
                var ticketPrice = new TicketPrice()
                {
                    TicketId = ticketList.FirstOrDefault().Id,
                    Price = 100,
                    Discount = 1,
                    UpperTime = DateTime.Now,
                    LowerTime = DateTime.Now.AddMonths(1),
                    Position = PositionEnum.Online,
                    IsEnabled = true,
                    Sort = 1,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.TicketPrice.Add(ticketPrice);
                _context.SaveChanges();
            }

            //票种详情插入
            var ticketIntroduceList = _context.TicketIntroduces.ToList();
            ticketList = _context.Tickets.ToList();
            scenicSpotList = _context.ScenicSpots.ToList();
            usersList = _context.Users.ToList();
            if (ticketIntroduceList.Count <= 0)
            {
                var ticketIntroduce = new TicketIntroduce()
                {
                    TicketId = ticketList.FirstOrDefault().Id,
                    ScenicSpotId = scenicSpotList.FirstOrDefault().Id,
                    TradeName = "FJ",
                    TradeInfo = "FJ",
                    Address = "FuJian",
                    CoverMap = "Fj.jpg",
                    Abstract = "fj",
                    Notice = "FJ",
                    RefundRule = "FJ",
                    IsOnLine = true,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    CreationTime = DateTime.Now,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.TicketIntroduces.Add(ticketIntroduce);
                _context.SaveChanges();
            }


            //站点插入
            scenicSpotList = _context.ScenicSpots.ToList();
            if (ticketStationList.Count <= 0)
            {
                var ticketStation = new TicketStation()
                {
                    ScenicSpotId = scenicSpotList.FirstOrDefault().Id,
                    StationName = "前门",
                    Remand = "test",
                    CreationTime = DateTime.Now,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.TicketStations.Add(ticketStation);
                _context.SaveChanges();
            }


            //站点可售票插入
            var ticketStationEnableList = _context.TicketStationEnables.ToList();
            ticketStationList = _context.TicketStations.ToList();
            usersList = _context.Users.ToList();
            ticketList = _context.Tickets.ToList();
            if (ticketStationEnableList.Count <= 0)
            {
                var ticketStationEnable = new TicketStationEnable()
                {
                    TicketId = ticketList.FirstOrDefault().Id,
                    StationId = ticketStationList.FirstOrDefault().Id,
                    CreationTime = DateTime.Now,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.TicketStationEnables.Add(ticketStationEnable);
                _context.SaveChanges();
            }


            //单据插入
            var activityList = _context.Activities.ToList();
            sourceList = _context.Sources.ToList();
            customerList = _context.Customers.ToList();
            activityList = _context.Activities.ToList();
            scheduleList = _context.Schedules.ToList();
            payMethodList = _context.PayMethods.ToList();
            usersList = _context.Users.ToList();
            if (activityList.Count <= 0)
            {
                var activity = new Activity()
                {
                    ActivityNo = CommonHelp.getVoucherNo(),
                    SourceId = sourceList.FirstOrDefault().Id,
                    ScheduleId = scheduleList.FirstOrDefault().Id,
                    //OrgActivityId = activityList.FirstOrDefault().Id,
                    ActivityDate = DateTime.Now,
                    PlayDate = DateTime.Now.AddDays(5),
                    CustomerId = customerList.FirstOrDefault().Id,
                    Buyer = customerList.FirstOrDefault().CustomerName,
                    Mobile = "18807330211",
                    PayMethodId = payMethodList.FirstOrDefault().Id,
                    VoucherNo = "现金",
                    PayStatus = PayStatusEnum.PayStatusPayment,
                    ActivityType = ActivityTypeEnum.ActivityTypePayment,
                    Remark = "",
                    IsPrint = true,
                    Closed = true,
                    TotalQuantity = 200,
                    TotalAmount = 400,
                    CreationTime = DateTime.Now,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.Activities.Add(activity);
                _context.SaveChanges();
            }


            //单据明细插入
            var activityDetailList = _context.ActivityDetails.ToList();
            activityList = _context.Activities.ToList();
            ticketPriceList = _context.TicketPrice.ToList();
            if (activityDetailList.Count <= 0)
            {
                var activityDetail = new ActivityDetail()
                {
                    ActivityId = activityList.FirstOrDefault().Id,
                    TicketPriceId = ticketPriceList.FirstOrDefault().Id,
                    Quantity = 100,
                    UnitPrice = 200,
                    Discount = 1,
                    SalePrice = 100,
                    TotalAmount = 100,
                    Remark = "",
                    //OrgDetailId=,
                    CreationTime = DateTime.Now,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.ActivityDetails.Add(activityDetail);
                _context.SaveChanges();
            }


            //账单插入
            var accountList = _context.Accounts.ToList();
            usersList = _context.Users.ToList();
            if (accountList.Count <= 0)
            {
                var account = new Account()
                {
                    AccountDate = DateTime.Now,
                    AccountNo = CommonHelp.getVoucherNo(),
                    Quantity = 300,
                    Amount = 300,
                    AccountStatus = AccountStatusEnum.IsAccount,
                    CollectiorId = usersList.FirstOrDefault().Id,
                    CollectionTime = DateTime.Now,
                    CreationTime = DateTime.Now,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                    PayMethodId = payMethodList.FirstOrDefault().Id,
                    RAmount = 0,
                    TotalAmount = 300,
                    Remark = "test",
                    VoucherNo = "",
                };
                _context.Accounts.Add(account);
                _context.SaveChanges();
            }


            //结账单明细插入
            var accountDetailList = _context.AccountDetails.ToList();
            usersList = _context.Users.ToList();
            accountList = _context.Accounts.ToList();
            ticketList = _context.Tickets.ToList();
            payMethodList = _context.PayMethods.ToList();
            if (accountDetailList.Count <= 0)
            {
                var accountDetail = new AccountDetail()
                {
                    AccountId = accountList.FirstOrDefault().Id,
                    ActivityDetailId = _context.ActivityDetails.FirstOrDefault().Id,
                    CreationTime = DateTime.Now,
                    CreatorUserId = user.Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.AccountDetails.Add(accountDetail);
                _context.SaveChanges();
            }


            //票据插入
            var ticketDetailList = _context.TicketDetails.ToList();
            activityDetailList = _context.ActivityDetails.ToList();
            activityList = _context.Activities.ToList();
            customerList = _context.Customers.ToList();
            ticketList = _context.Tickets.ToList();
            routeList = _context.Routes.ToList();
            boatList = _context.Boats.ToList();
            scheduleList = _context.Schedules.ToList();
            if (ticketDetailList.Count <= 0)
            {
                var ticketDetail = new TicketDetail()
                {
                    ActivityDetailId = activityDetailList.FirstOrDefault().Id,
                    ActivityId = activityList.FirstOrDefault().Id,
                    TicketNo = CommonHelp.getVoucherNo(),
                    //OrgActivityDetailId
                    //OrgActivityId      
                    CustomerId = customerList.FirstOrDefault().Id,
                    TicketId = ticketList.FirstOrDefault().Id,
                    //TicketNo=,
                    QRCode = CommonHelp.getVoucherNo(),
                    CheckingQuantity = 100,
                    CheckedQuantity = 100,
                    StartDateTime = DateTime.Now,
                    EndDateTime = DateTime.Now.AddMonths(1),
                    CheckMethod = CheckMethodEnum.BalanceCheck,
                    RouteId = routeList.FirstOrDefault().Id,
                    ScheduleId = scheduleList.FirstOrDefault().Id,
                    BoatId = boatList.FirstOrDefault().Id,
                    TicketStatus = TicketStatusEnum.Activate,
                    CheckTime = DateTime.Now,
                    CreationTime = DateTime.Now,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.TicketDetails.Add(ticketDetail);
                _context.SaveChanges();
            }


            //验票记录插入
            var ticketDetailHistoryList = _context.TicketDetailHistorys.ToList();
            devicesList = _context.Devices.ToList();
            ticketList = _context.Tickets.ToList();
            ticketDetailList = _context.TicketDetails.ToList();
            customerList = _context.Customers.ToList();
            if (ticketDetailHistoryList.Count <= 0)
            {
                var ticketDetailHistory = new TicketDetailHistory()
                {
                    TicketId = ticketList.FirstOrDefault().Id,
                    DeviceId = devicesList.FirstOrDefault().Id,
                    DeviceName = devicesList.FirstOrDefault().DeviceName,
                    TicketDetailId = ticketDetailList.FirstOrDefault().Id,
                    //TicketNo
                    CustomerId = customerList.FirstOrDefault().Id,
                    VisitValue = "a",
                    TicketName = ticketList.FirstOrDefault().TicketName,
                    ValidationType = "a",
                    StatusCode = CheckStatusEnum.CheckFalse,
                    StatusText = "a",
                    CheckDate = DateTime.Now,
                    Year = DateTime.Now.Year,
                    Month = DateTime.Now.Month,
                    Day = DateTime.Now.Day,
                    Hour = DateTime.Now.Hour,
                    YearWeek = 2,
                    WeekDay = 1,
                    CheckTime = DateTime.Now,
                    CreationTime = DateTime.Now,
                    CreatorUserId = usersList.FirstOrDefault().Id,
                    BranchId = branchList.FirstOrDefault().Id,
                };
                _context.TicketDetailHistorys.Add(ticketDetailHistory);
                _context.SaveChanges();
            }

            //暂存订单插入
            //var activityTempList = _context.ActivityTemps.ToList();
            //sourceList = _context.Sources.ToList();
            //activityList = _context.Activities.ToList();
            //usersList = _context.Users.ToList();
            //customerList = _context.Customers.ToList();
            //if (activityTempList.Count <= 0)
            //{
            //    var activityTemp = new ActivityTemp()
            //    {
            //        SourceId = sourceList.FirstOrDefault().Id,
            //        OrderType = OrderTypeEnum.Customer,
            //        PayStatus = PayStatusEnum.Payment,
            //        CustomerId = customerList.FirstOrDefault().Id,
            //        ActivityDate = DateTime.Now,
            //        PlayDate = DateTime.Now,
            //        Buyer = usersList.FirstOrDefault().Name,
            //        Mobile = "17303223156",
            //        PayMethodId = payMethodList.FirstOrDefault().Id,
            //        Remark = "",
            //        TotalQuantity = 100,
            //        TotalAmount = 100,
            //        CollectionPwd = "",
            //        Collector = usersList.FirstOrDefault().Name,
            //        CollectionCode = "",
            //        PayTime = DateTime.Now,
            //        ActivityId = activityList.FirstOrDefault().Id,
            //        CreationTime = DateTime.Now,
            //        CreatorUserId = usersList.FirstOrDefault().Id,
            //        BranchId = branchList.FirstOrDefault().Id,
            //    };
            //    _context.ActivityTemps.Add(activityTemp);
            //    _context.SaveChanges();
            //}


            ////暂存订单明细插入
            //var activityTempDetailList = _context.ActivityTempDetails.ToList();
            //activityTempList = _context.ActivityTemps.ToList();
            //ticketPriceList = _context.TicketPrice.ToList();
            //if (activityTempDetailList.Count <= 0)
            //{
            //    var activityTempDetail = new ActivityTempDetail()
            //    {
            //        ActivityTempId = activityTempList.FirstOrDefault().Id,
            //        TicketPriceId = ticketPriceList.FirstOrDefault().Id,
            //        TicketName = "a",
            //        SalePrice = 100,
            //        Quantity = 100,
            //        UniPrice = 100,
            //        Discount = 100,
            //        CreationTime = DateTime.Now,
            //        CreatorUserId = usersList.FirstOrDefault().Id,
            //        BranchId = branchList.FirstOrDefault().Id,
            //    };
            //    _context.ActivityTempDetails.Add(activityTempDetail);
            //    _context.SaveChanges();
            //}


            //旅行社购票明细插入
            //var travelTicketDetailList = _context.TravelTicketDetails.ToList();
            //customerList = _context.Customers.ToList();
            //travelAgencyList = _context.TravelAgencys.ToList();
            //activityTempDetailList = _context.ActivityTempDetails.ToList();
            //if (travelTicketDetailList.Count <= 0)
            //{
            //    var travelTicketDetail = new TravelTicketDetail()
            //    {
            //        CustomerId = customerList.FirstOrDefault().Id,
            //        TravelAgencyId = travelAgencyList.FirstOrDefault().Id,
            //        ActivityTempDetailId = activityTempDetailList.FirstOrDefault().Id,
            //        CreationTime = DateTime.Now,
            //        CreatorUserId = usersList.FirstOrDefault().Id,
            //        BranchId = branchList.FirstOrDefault().Id,
            //    };
            //    _context.TravelTicketDetails.Add(travelTicketDetail);
            //    _context.SaveChanges();
            //}
        }
    }
}